jQuery.fn.reset = function () {
    $(this).each (function() {
        this.reset();
        var $inputs = $('#'+$(this).attr('id')+' :input');
        $inputs.each(function() {
            if($(this).attr('type')=='select-one'){
                document.getElementById($(this).attr('id')).selectedIndex = 0;
            }
        });
    });
}

function loadUrl(options){
    options = $.extend({
        id: "",
        url: "",
        parameters: {},
        loader: "loader",
        func: function(){},
        center_on_complete: false
    }, options);

    //alert("2: "+options['id']);

    var element;
    if(typeof(options['id']) != "object")
    {
        element = $('#'+options['id']);
    }
    else{
        element = options['id'];
    }

    var tiene_loader = false;
    if(options['loader']!=undefined)
    {
        tiene_loader = true;
    }

    var es_chico = true;
    if(element.width()>150 || element.height()>50){
        es_chico = false;
    }

    if(es_chico){
        element.fadeOut('fast',loadContent);
    }
    else{
        element.fadeOut('normal',loadContent);
    }
    if(!es_chico || tiene_loader){
        $('#'+options['loader']).remove();
        element.after('<img id="'+options['loader']+'" src="/images/loader.gif" alt ="loading"/>');
    }

    function loadContent() {
        element.load(
            options['url'],
            options['parameters'],
            showNewContent)
    }
    function showNewContent() {
        if(element.width()>100 && element.height()>100){
            es_chico = false;
        }

        if(es_chico){
            element.fadeIn('fast',hideLoader);
        }
        else{
            element.fadeIn('normal',hideLoader);
            if(options['center_on_complete']){
                var position = element.offset().top-$(window).height()/4;
                if(position < 0)
                    position = 0;
                $(window).scrollTo(position, 800);
            }
        }

    //window.scrollTo(0, element.offset().top);
    }
    function hideLoader() {
        $('#'+options['loader']).hide();
        options['func']();
    }
    return false;
}

function callUrl(id,url,parameters,loader, func){
    //alert("id: "+id+" ## url:"+url+" ## values: ");
    loadUrl(
    {
        id: id,
        url: url,
        parameters: parameters,
        loader: loader,
        func: func
    });
}

function callAction(id, url, form_id, loader){

    if(form_id==undefined)
    {
        form_id = "form";
    }


    //alert('id: ' + id + '; form: ' + form_id);
    var $inputs = $('#'+form_id+' :input');

    var values = {};
    
    $inputs.each(function() {
        //alert("0");
        if($(this).attr('type')=='checkbox'){
            if($(this).attr('checked'))
                values[this.name] = 1;
            else
                values[this.name] = 0;
        }
        else if($(this).attr('type')=='radio'){
            if($(this).attr('checked'))
                values[this.name] = $(this).val();
        }
        else{
            values[this.name] = $(this).val();
        }
    });
    //alert(id+" ## url:"+url+" ## values: "+values[0]);

    loadUrl(
    {
        id: id,
        url: url,
        parameters: values,
        loader: loader
    });
}

function callFormAction(id, form_id, loader){
    if(form_id==undefined)
    {
        form_id = "form";
    }

    var url = $('#'+form_id).attr('action');


    callAction(id, url, form_id, loader);
}
